package mainWindowSupport;

import java.util.List;

import javax.swing.JTable;
import javax.swing.event.TableModelListener;

import actionHandlers.PropTableColorListener;
import actionHandlers.PropertyModelListener;

import cuShapes.Arrow;
import cuShapes.PQShape;

public class ShapePropTable {

	JTable propTable;
	TableModelListener propModelListener;
	PropTableColorListener propColorListener;
	
	public ShapePropTable(List<String> propRows) {
			
		propTable = new JTable();
		propModelListener = new PropertyModelListener();
		propColorListener = new PropTableColorListener();
		initComponents(propRows);

	}
	
	@SuppressWarnings("serial")
	private void initComponents(List<String> numRows) {

		// if size of numRows is 6 then we are making a table for a shape
		if(numRows.size() == 6) {
	        propTable.setModel(new javax.swing.table.DefaultTableModel(
	            new Object [numRows.size()][2],
	            new String [] {"General", ""}
	        ) {
	            Class<?>[] types = new Class<?>[] {
	                java.lang.String.class, java.lang.String.class
	            };
	            boolean[] canEdit = new boolean [] {
	                false, true
	            };
	
	            public Class<?> getColumnClass(int columnIndex) {
	                return types [columnIndex];
	            }
	
	            public boolean isCellEditable(int rowIndex, int columnIndex) {
	                if(rowIndex == 5)
	                	return false;
	                else 
	                	return canEdit [columnIndex];
	            }
	        });
	        for(int i=0;i<numRows.size();i++)
	        	propTable.getModel().setValueAt(numRows.get(i), i, 0);
	    // if size of numRows is 2 then we are making a table for an arrow
		} else if (numRows.size() == 2) {
			propTable.setModel(new javax.swing.table.DefaultTableModel(
		            new Object [numRows.size()][2],
		            new String [] {"General", ""}
		        ) {
		            Class<?>[] types = new Class<?>[] {
		                java.lang.String.class, java.lang.String.class
		            };
		            boolean[] canEdit = new boolean [] {
		                false, true
		            };
		
		            public Class<?> getColumnClass(int columnIndex) {
		                return types [columnIndex];
		            }
		
		            public boolean isCellEditable(int rowIndex, int columnIndex) {
		            	if(rowIndex == 1)
		            		return false;
		            	else
		            		return canEdit [columnIndex];
		            }
		        });
		        for(int i=0;i<numRows.size();i++)
		        	propTable.getModel().setValueAt(numRows.get(i), i, 0);
		}
    }
	
	public JTable getCurrentTable() {
		return propTable;
	}
	
	public void setProperties(PQShape shape) {
		//double width = Math.abs(Math.abs(shape.getStart().getX()) - Math.abs(shape.getEnd().getX()));
		//double height = Math.abs(Math.abs(shape.getStart().getY()) - Math.abs(shape.getEnd().getY()));
		//double length = 0;
		
		if(shape instanceof Arrow) {
			//length = Math.sqrt(width*width + height*height);
			//propTable.getModel().setValueAt(String.valueOf(length), 3, 1);
			propTable.getModel().setValueAt(shape.getColor(), 1, 1);
		} else {
			propTable.getModel().setValueAt(String.valueOf(shape.getWidth()), 3, 1);
			propTable.getModel().setValueAt(String.valueOf(shape.getHeight()), 4, 1);
			propTable.getModel().setValueAt(shape.getColor(), 5, 1);
			propTable.getModel().setValueAt(String.valueOf(shape.getStart().getX()), 1, 1);
			propTable.getModel().setValueAt(String.valueOf(shape.getStart().getY()), 2, 1);
			//shapeTable.updateUI();
		}
		
		propTable.getModel().setValueAt(shape.getName(), 0, 1);
		
	}
	
	public void clear() {
		for(int i=0; i<propTable.getModel().getRowCount();i++)
			propTable.getModel().setValueAt("", i, 1);

		propTable.updateUI();
	}
	
	public void addTableModelListener() {
		propTable.getModel().addTableModelListener(propModelListener);
		propTable.addMouseListener(propColorListener);
	}
	public void removeTableModelListener() {
		propTable.getModel().removeTableModelListener(propModelListener);
		propTable.removeMouseListener(propColorListener);
	}
}
